package com.hdoit.apollo.web.cargo.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tennetcn.web.message.WebManageContainer;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.authority.component.web.ActionResult;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.ErrorResult;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.hdoit.apollo.logical.cargo.model.Standard;
import com.hdoit.apollo.logical.cargo.service.IStandardService;
import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.viewmodel.cargo.StandardSearch;

/**
 * @author      humaoshu
 * @email       
 * @createtime  2018年04月26日 11:29:24
 * @comment
 */
@Controller
@RequestMapping(value = "/cargo/standard/")
public class StandardController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/cargo/standard/";
    }

    @Autowired
    private IStandardService standardService;

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="detailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,StandardSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(standardService.queryCountMP(search));
        List<Standard> standards= standardService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("standards", standards);
        model.addAttribute("standardSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String standardDetailed(Model model,String key){
        Standard standard=new Standard();
        if(!StringUtils.isEmpty(key)){
            standard=standardService.queryModel(key);
        }
        model.addAttribute("standard", standard);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Standard standard){
        if(StringUtils.isEmpty(standard.getStandardId())){
            standard.setStandardId(CommonUtils.getKeyId());
            standard.setCreateDate(DateUtils.getCurrent());
            standard.setDeleteMark(YesOrNoInteger.no);
            standard.setCreateUserId(getLoginModel().getLoginId());
            standard.setCreateUserName(getLoginModel().getLoginName());
            standard.setModelStatus(ModelStatus.add);
            Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
            standard.setShopId(shop.getShopId());
        }else{
            standard.setModifyDate(DateUtils.getCurrent());
            standard.setModifyUserId(getLoginModel().getLoginId());
            standard.setModifyUserName(getLoginModel().getLoginName());
            standard.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(standardService.applyChange(standard)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("standard", standard);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(standardService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}